package cn.myzf.fort.fortaccess.dto;

import cn.myzf.fort.access.accesscommon.core.ChannelProperties;
import cn.myzf.fort.access.accesscommon.dto.HostInfo;
import cn.myzf.fort.access.accesscommon.utils.JsonUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;


@Component
public class CMDBProperties implements ChannelProperties {

    public CMDBProperties() {


    }



    @Override
    public List<HostInfo> list() {
        List<HostInfo> list = new ArrayList<>();
        try {
            Map o = JsonUtils.readJsonFromClassPath("cmdb.json", Map.class);
            if (!o.isEmpty()) {
                JSONArray results = (JSONArray) o.get("results");
                if (results!=null && results.size() > 0 )
                 list = JSONObject.parseArray(results.toString(),HostInfo.class);
               /* if (list.size()>0) {
                    Yaml yaml = new Yaml();
                    File directory = new File("src/main/resources/");
                    String reportPath = directory.getCanonicalPath()  + File.separator;
                    yaml.dump(list,new FileWriter(reportPath +"application-cmdb.yml"));
                }*/
            }
        } catch (IOException e) {

            e.printStackTrace();
        }

        return list;

    }
}
